<?php

namespace App\Http\Controllers;

use Illuminate\Support\Facades\Storage;
use Ramsey\Uuid\Uuid;
use App\Models\Notification;
use App\Http\Requests\Notification\StoreNotificationResource;
use App\Http\Requests\Notification\UpdateNotificationResource;
use App\Http\Requests\Notification\DestroyNotificationResource;
use App\Http\Requests\Notification\QueryNotificationCollection;

class NotificationController extends Controller
{
	/**
	 * Date: 2018/2/28
	 * @author George
	 * @param QueryNotificationCollection $request
	 * @return \Illuminate\Http\JsonResponse
	 */
    public function index(QueryNotificationCollection $request)
    {
        // 分页数
        $paginate = $request->get('paginate', 10);

        $query = Notification::query();

        return $this->success($query->orderBy('created_at','desc')->paginate($paginate));
    }

	/**
	 * 创建公告
	 *
	 * Date: 2018/2/28
	 * @author George
	 * @param StoreNotificationResource $request
	 * @return \Illuminate\Http\JsonResponse
	 * @throws \Throwable
	 */
    public function store(StoreNotificationResource $request)
    {
        $notification = new Notification();

        $attributes = $this->validate($request, [
            'title' => 'required',
			'content' => 'required',
            'path' => 'nullable'
        ],[
            'title.required' => '标题不能为空',
            'content.required' => '内容不能为空',
        ]);

        $notification->data = json_encode($attributes);

        $notification->id = Uuid::uuid4();
        $notification->type = "public";

        $notification->saveOrFail();
        return $this->stored($notification);
    }

	/**
	 * Date: 2018/2/28
	 * @author George
	 * @param Notification $notification
	 * @return \Illuminate\Http\JsonResponse
	 */
    public function show(Notification $notification)
    {
        return $this->success($notification);
    }

	/**
	 * 更新通知信息
	 *
	 * Date: 2018/2/28
	 * @author George
	 * @param UpdateNotificationResource $request
	 * @param Notification $notification
	 * @return \Illuminate\Http\JsonResponse
	 * @throws \Throwable
	 */
    public function update(UpdateNotificationResource $request, Notification $notification)
    {
        $attributes = $this->validate($request, [
			'title' => 'required',
			'content' => 'required',
            'path' => 'nullable'
        ],[
			'title.required' => '标题不能为空',
			'content.required' => '内容不能为空',
		]);

        $data = json_decode($notification->data, true);
        if(($request->input('path') == "" || $request->input('path') == "null")
            && $data['path'] != "" &&  $data['path'] != "null" ){
            Storage::disk('public')->delete(str_replace('/storage', '', $data['path']));
        }

        $notification->data = json_encode($attributes);

        $notification->saveOrFail();
        return $this->stored($notification);
    }

	/**
	 * Date: 2018/2/28
	 * @author George
	 * @param DestroyNotificationResource $request
	 * @param Notification $notification
	 * @return \Illuminate\Http\JsonResponse
	 * @throws \Exception
	 */
    public function destroy(DestroyNotificationResource $request, Notification $notification)
    {
        $notification->delete();
        return $this->deleted();
    }
}
